package com.xiyu.service.service.files;

import com.xiyu.service.vo.files.housingAgreement.HousingAgreementSingleGetOutput;
import com.xiyu.service.vo.files.housingAgreement.HousingAgreementUpdateInput;
import com.xiyu.service.vo.files.housingAgreement.HousingAgreementCreateInput;
import com.xiyu.service.model.files.agreement.FilesHousingAgreement;
import com.xiyu.service.repository.files.agreement.FilesHousingAgreementRepository;
import com.xiyu.service.vo.files.housingAgreement.HousingAgreementPageQueryOutput;
import com.xiyu.service.vo.files.housingAgreement.HousingAgreementPageQueryInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.util.entity.EntityUtils;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.files.HousingAgreementConvert;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.files.HousingAgreementErrorCode.*;

/**
 * 用房协议 Service 实现类
 */
@Service
@Validated
public class HousingAgreementServiceImpl implements HousingAgreementService {

    @Resource
    private FilesHousingAgreementRepository filesHousingAgreementRepository;




    @Override
    public PageResult<HousingAgreementPageQueryOutput> pageQuery(HousingAgreementPageQueryInput inputVO) {
        Page<FilesHousingAgreement> pageFilesHousingAgreement = filesHousingAgreementRepository.pageQuery(inputVO);
        List<HousingAgreementPageQueryOutput> listFilesHousingAgreement = HousingAgreementConvert.INSTANCE.pageQueryOutputConvert(pageFilesHousingAgreement.getRows());
        return new PageResult<>(listFilesHousingAgreement, pageFilesHousingAgreement.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(HousingAgreementCreateInput inputVO) {
        FilesHousingAgreement newFilesHousingAgreement = HousingAgreementConvert.INSTANCE.createInputConvert(inputVO);
        newFilesHousingAgreement = filesHousingAgreementRepository.insert(newFilesHousingAgreement);
        return newFilesHousingAgreement.id();

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(HousingAgreementUpdateInput inputVO) {
        Optional<FilesHousingAgreement> optionalOldFilesHousingAgreement = filesHousingAgreementRepository.findById(inputVO.getId());
        if(!optionalOldFilesHousingAgreement.isPresent()){
            throw exception(FILES_HOUSING_AGREEMENT_NOT_EXIST);
        }
        FilesHousingAgreement updateFilesHousingAgreement = HousingAgreementConvert.INSTANCE.updateInputConvert(inputVO);
        if (!EntityUtils.isEquals(optionalOldFilesHousingAgreement.get(), updateFilesHousingAgreement))
            filesHousingAgreementRepository.update(updateFilesHousingAgreement);
        return true;
    }

    @Override
    public HousingAgreementSingleGetOutput singleGet(Long id) {
        Optional<FilesHousingAgreement> optionFilesHousingAgreement = filesHousingAgreementRepository.findById(id);
        if(!optionFilesHousingAgreement.isPresent()){
            throw exception(FILES_HOUSING_AGREEMENT_NOT_EXIST);
        }
        return HousingAgreementConvert.INSTANCE.singleGetOutputConvert(optionFilesHousingAgreement.get());

    }

}
